package com.hyacinth.lotterysystem.service.activityStatus.operater;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.hyacinth.lotterysystem.dao.entity.ActivityPrizeDO;
import com.hyacinth.lotterysystem.dao.mapper.ActivityPrizeMapper;
import com.hyacinth.lotterysystem.service.activityStatus.ConvertActivityStatusDTO;
import com.hyacinth.lotterysystem.service.enums.ActivityPrizeStatusEnum;
import jakarta.annotation.Resource;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;

@Component
public class PrizeOperator extends AbstractActivityOperator {
    private static final Logger logger = LoggerFactory.getLogger(PrizeOperator.class);
    @Resource
    private ActivityPrizeMapper activityPrizeMapper;


    @Override
    public Integer sequence() {
        return 1;
    }

    @Override
    public Boolean needConvert(ConvertActivityStatusDTO convertActivityStatusDTO) {
        Long prizeId = convertActivityStatusDTO.getPrizeId();
        Long activityId = convertActivityStatusDTO.getActivityId();

        ActivityPrizeStatusEnum prizeStatusEnum = convertActivityStatusDTO.getPrizeStatusEnum();
        if (prizeId == null || prizeStatusEnum == null || activityId == null)
            return false;
        LambdaQueryWrapper<ActivityPrizeDO> activityPrizeDOLambdaQueryWrapper = Wrappers.lambdaQuery(ActivityPrizeDO.class)
                .eq(ActivityPrizeDO::getActivityId, activityId)
                .eq(ActivityPrizeDO::getPrizeId, prizeId);
        ActivityPrizeDO activityPrizeDO = activityPrizeMapper.selectOne(activityPrizeDOLambdaQueryWrapper);
        if (activityPrizeDO == null || prizeStatusEnum.name().equalsIgnoreCase(activityPrizeDO.getStatus()))
            return false;

        return true;
    }

    @Override
    public Boolean convert(ConvertActivityStatusDTO convertActivityStatusDTO) {
        LambdaUpdateWrapper<ActivityPrizeDO> activityPrizeDOLambdaUpdateWrapper = Wrappers.lambdaUpdate(ActivityPrizeDO.class)
                .eq(ActivityPrizeDO::getActivityId, convertActivityStatusDTO.getActivityId())
                .eq(ActivityPrizeDO::getPrizeId, convertActivityStatusDTO.getPrizeId())
                .set(ActivityPrizeDO::getStatus, convertActivityStatusDTO.getPrizeStatusEnum().name());
        try {
            activityPrizeMapper.update(activityPrizeDOLambdaUpdateWrapper);
        } catch (Exception e) {
            return false;
        }
        logger.info("activityPrize 活动奖品状态扭转了");
        return true;
    }
}
